<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>角色管理</title>
		<link href="../../component/pear/css/pear.css" rel="stylesheet" />
	</head>
	<body class="pear-container">
	<!--信息展示表格-->
		<div class="layui-card">
			<div class="layui-card-body">
				<table id="role-table" lay-filter="role-table"></table>
			</div>
		</div>
		<!--新增/批量删除组件-->
		<script type="text/html" id="role-toolbar">
			<button class="pear-btn pear-btn-primary pear-btn-md" lay-event="add">
				<i class="layui-icon layui-icon-add-1"></i>
				新增
			</button>
			<button class="pear-btn pear-btn-danger pear-btn-md" lay-event="batchRemove">
				<i class="layui-icon layui-icon-delete"></i>
				删除
			</button>
		</script>
	<!--启用禁用组件-->
	<script type="text/html" id="user-enable">
		<input type="checkbox" name="enable" value="{{d.uid}}" lay-skin="switch" lay-text="启用|禁用" lay-filter="user-enable" {{d.enable==1?'checked':''}}>
	</script>
		<!--修改/删除组件-->
		<script type="text/html" id="role-bar">
			<button class="pear-btn pear-btn-primary pear-btn-sm" lay-event="edit"><i class="layui-icon layui-icon-edit"></i></button>
			<button class="pear-btn pear-btn-danger pear-btn-sm" lay-event="remove"><i class="layui-icon layui-icon-delete"></i></button>
		</script>
	<!--新增/修改表单组件-->
	<div style="display: none" id="showRole">
		<form class="layui-form" action="" lay-filter="roleForm">
			<input type="hidden" name="roleid">
			<div class="mainBox">
				<div class="main-container">
					<div class="main-container">
						<div class="layui-form-item">
							<label class="layui-form-label">角色</label>
							<div class="layui-input-block">
								<input type="text" name="rolename" lay-verify="title" autocomplete="off" placeholder="请输入标题" class="layui-input">
							</div>
						</div>
						<div class="layui-form-item">
							<label class="layui-form-label">描述</label>
							<div class="layui-input-block">
								<input type="text"  name="details" lay-verify="title" autocomplete="off" placeholder="请输入标题" class="layui-input">
							</div>
						</div>
					</div>
				</div>
			</div>
			<div class="bottom">
				<div class="button-container">
					<button type="submit" class="pear-btn pear-btn-primary pear-btn-sm" lay-submit="" lay-filter="role-submit">
						<i class="layui-icon layui-icon-ok"></i>
						提交
					</button>
					<button type="reset" class="pear-btn pear-btn-sm">
						<i class="layui-icon layui-icon-refresh"></i>
						重置
					</button>
				</div>
			</div>
		</form>
	</div>

		<script src="../../component/layui/layui.js"></script>
		<script src="../../component/pear/pear.js"></script>
		<script>
		    layui.use(['table','form','jquery'],function () {
		        let table = layui.table;
		        let form = layui.form;
		        let $ = layui.jquery;
		        let MODULE_PATH = "http://localhost:8080/auths/";
				let cols = [
					[{
						type: 'checkbox'
					},
						{
							title: '序号',
							field: 'id',
							align: 'center',
							width: 300
						},
						{
							title: '角色名',
							field: 'rolename',
							align: 'center',
							width: 300
						},
						{
							title: '描述',
							field: 'details',
							align: 'center'
						},
						{
							title: '操作',
							field: 'checked',
							align: 'center',
							templet: '#user-enable'

						},
					]

				]
				table.render({
					elem: '#role-table',
					url: 'http://localhost/getlist5',
					page: true,
					cols: cols,
					skin: 'line',
					limit:2,
					limits:[2,4,6,8,10],
					toolbar: '#role-toolbar',
					defaultToolbar: [{
						title: '刷新',
						layEvent: 'refresh',
						icon: 'layui-icon-refresh',
					}, 'filter', 'print', 'exports']
				});

		        table.on('tool(role-table)', function(obj){
		            if(obj.event === 'remove'){
		                window.remove(obj);
		            } else if(obj.event === 'edit'){
		                window.edit(obj);
		            } else if(obj.event === 'power'){
		                window.power(obj);
		            }
		        });

		        table.on('toolbar(role-table)', function(obj){
		            if(obj.event === 'add'){
		                window.add();
		            } else if(obj.event === 'refresh'){
		                window.refresh();
		            } else if(obj.event === 'batchRemove'){
		                window.batchRemove(obj);
		            }
		        });
		        let index = 0;
		        window.add = function(){
					var data = data //获得当前行数据
							,layEvent = event; //获得 lay-event 对应的值
					var form = layui.form;
					layer.open({
						type: 1,
						title: '文章添加',
						area: ['500px', '500px'],
						anim: 5,
						resize: false,
						content: $("#showRole")
					});
					form.on('submit(role-submit)', function(data){
						$.ajax({
							url:"http://localhost/insert5",
							data:data.field,
							dataType:"json",
							type:"POST",
							success:function (d) {
								if (z.data > 0) {
									table.reload('role-table')
								}
							}
						})
					});
		        }
		        window.edit = function(obj){
                    index = layer.open({
                        type: 1,
                        title: '修改权限',
                        area: ['500px', '300px'],
                        content: $('#showRole')
                    });
                    form.on('submit(role-submit)', function(data){
                        layer.close(index);
                    });
		        }

		        window.remove = function(obj){
		            layer.confirm('确定要删除该角色', {icon: 3, title:'提示'}, function(index){
		                layer.close(index);
		                let loading = layer.load();
		                $.ajax({
		                    url: MODULE_PATH+"remove/"+obj.data['roleId'],
		                    dataType:'json',
		                    type:'delete',
		                    success:function(result){
		                        layer.close(loading);
		                        if(result.success){
		                            layer.msg(result.msg,{icon:1,time:1000},function(){
		                                obj.del();
		                            });
		                        }else{
		                            layer.msg(result.msg,{icon:2,time:1000});
		                        }
		                    }
		                })
		            });
		        }

		        window.batchRemove = function(obj){

					var checkStatus = table.checkStatus(obj.config.id)
							,data = checkStatus.data; //获取选中的数据
					if (data.length === 0) {
						layer.msg('请选择一行');
					} else {
						var arr = new Array();
						for (var i = 0; i < checkStatus.data.length; i++) {
							arr.push(checkStatus.data[i].id);
						}
						$.ajax({
							url: 'http://localhost/Bydelete5'
							, data: {"ids": arr}
							, traditional: true  //如果向后台传输数组或者list的对象的时候要深度序列化
							, success: function () {
									table.reload('role-table')
							}

						})
					}
				}

				window.refresh = function(param) {
					table.reload('role-table');
				}
		    })
		</script>
	</body>
</html>
